package com.octopus.media.fuwu;

import android.app.Service;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import java.util.Random;

public class Monitoring extends Service {

    // Binder given to clients
    private final IBinder mBinder = new MonitoringBinder();
    // Random number generator
    private final Random mGenerator = new Random();
    private final int JoinThreadTime = 1000;
    private MonitoringThread monitoringThread;
    private boolean isrun = true;
    private boolean associatedRun;
    //经纬度
    private double lat=0, lon=0;

    private BroadcastReceiver myReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null) return;
            if(intent.getAction().equals("com.octopus.associated.aidl")){
                Log.i("Monitoring","associated return me msg");
                associatedRun = true;
            }else if(intent.getAction().equals("com.octopus.media.jingwei")){
                Intent intentjy = new Intent("com.octopus.media.baiduJy");
                intentjy.putExtra("LAT",lat);
                intentjy.putExtra("LON",lon);
                sendBroadcast(intentjy);
            }
        }
    };

    public class MonitoringBinder extends Binder {
        public Monitoring getService() {
            // Return this instance of LocalService so clients can call public methods
            return Monitoring.this;
        }
    }


    @Override
    public void onCreate() {
        super.onCreate();

        IntentFilter filter = new IntentFilter();
        filter.addAction("com.octopus.associated.aidl");
        filter.addAction("com.octopus.media.jingwei");
        registerReceiver(myReceiver, filter);

        associatedRun = true;
        Intent sendMsg = new Intent("com.octopus.media.aidl");
        sendBroadcast(sendMsg);

        isrun = true;
        monitoringThread = new MonitoringThread(Monitoring.this);
        monitoringThread.start();
        Log.i("Monitoring","MonitoringThread start");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onDestroy() {
        try {
            unregisterReceiver(myReceiver);
            isrun = false;
            monitoringThread.join(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Log.i("Monitoring","MonitoringThread join");
        super.onDestroy();
    }

    public class MonitoringThread extends Thread{

        private Context context;

        public MonitoringThread(Context context){
            this.context = context;
            Log.i("Monitoring","MonitoringThread build");
        }

        @Override
        public void run() {
            super.run();
            Log.i("Monitoring","MonitoringThread run");

            while (isrun){
                Log.i("Monitoring","MonitoringThread loop");
                try {
                    if (associatedRun){
                        associatedRun = false;
                        Intent sendMsg = new Intent("com.octopus.media.aidl");
                        sendBroadcast(sendMsg);
                        Log.i("Monitoring","call associated");
                    }else{
                        goActivity();
                    }

                    Thread.sleep(JoinThreadTime * 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void goActivity(){
            try {
                Log.i("Monitoring","associated not running");
                Intent intent = new Intent();
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setClassName("com.octopus.associated", "com.octopus.associated.MainActivity");
                startActivity(intent);
            }catch (ActivityNotFoundException exception){
                exception.printStackTrace();
            }
        }
    }
}
